package com.springboot.demo.threadpool;

import ch.qos.logback.core.util.TimeUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ExecutorTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        Runnable run = () -> {
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        };
        Callable cun = () -> {
          TimeUnit.MILLISECONDS.sleep(1000);
            return "static";
        };

        List<Callable<String>> arr = new ArrayList<>();
        arr.add(cun);
        arr.add(cun);
        arr.add(cun);

        executorService.submit(run);
        Future submit = executorService.submit(cun);

        String s = executorService.invokeAny(arr);
        System.out.println(s);
        List<Future<String>> futures = executorService.invokeAll(arr);
        futures.forEach(System.out::println);

        executorService.shutdown();
        if (!executorService.awaitTermination(2000,TimeUnit.MILLISECONDS)){
            System.out.println("ShutDown service");
        }
    }
}
